//
//  CommentsTableViewCell.m
//  XDBaseFrameWork
//
//  Created by wanglong8889@126.com on 14-9-30.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//  60 10 x 20 70 20

#import "CommentsTableViewCell.h"

@implementation CommentsTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
        _headView.backgroundColor = UIColorFromRGB(0xeaeaea);
        _headView.layer.cornerRadius = 25;
        _headView.layer.masksToBounds = YES;
        _headView.image = [UIImage imageNamed:@"placeholderHeader"];
        [self.contentView addSubview:_headView];
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10+_headView.frame.origin.x+_headView.frame.size.width, 8, 100, 23)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.font = [UIFont systemFontOfSize:14.0];
        _nameLabel.textColor = UIColorFromRGB(0x453d35);
        [self.contentView addSubview:_nameLabel];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x+_nameLabel.frame.size.width+20,8,20*6, 23)];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.font = [UIFont systemFontOfSize:14.0];
        _timeLabel.textColor = UIColorFromRGB(0xd7d6d6);
        [self.contentView addSubview:_timeLabel];

        
//        self.starView = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(width_x(_headView)-8,height_y(_timeLabel)+5,20*6, 23) andStars:5 isFractional:NO];
        self.starView = [[DLStarRatingControl alloc] initWithFrame:CGRectMake(width_x(_headView)-12,height_y(_timeLabel)+5,20*6, 20) andStars:5 isFractional:NO];
        _starView.backgroundColor = [UIColor clearColor];
        _starView.rating = 0;

        _starView.enabled = NO;
        
        [self.contentView addSubview:_starView];
        
        
        self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(_starView.frame.origin.x+_starView.frame.size.width+5, height_y(_timeLabel)+5, 25, _starView.frame.size.height)];
        _countLabel.backgroundColor = [UIColor clearColor];
        _countLabel.font = [UIFont systemFontOfSize:15.0];
        _countLabel.textColor = UIColorFromRGB(0xffa200);
        _countLabel.text = @"";
        [self.contentView addSubview:_countLabel];
        
        
//        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-150,_countLabel.frame.origin.y+_countLabel.frame.size.height, 150, 23)];
//        _timeLabel.backgroundColor = [UIColor clearColor];
//        _timeLabel.font = [UIFont systemFontOfSize:14.0];
//        _timeLabel.textColor = RGBA(229, 49, 94,1.0);
//        _timeLabel.text = @"2014-08-09 12:24";
//        [self addSubview:_timeLabel];
        
        
        
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_headView.frame.origin.x,10+_headView.frame.size.height+_headView.frame.origin.y, 300, 23)];
        _contentLabel.font = [UIFont systemFontOfSize:15.0];
        _contentLabel.textColor = RGBA(229, 49, 94,1.0);
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = UIColorFromRGB(0x453d35);
        _contentLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_contentLabel];
        
        
        
        self.downLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0,200-.5, UI_SCREEN_WIDTH, .5)];
        _downLineView.backgroundColor = UIColorFromRGB(0xd4ccc5);
        
        [self.contentView addSubview:_downLineView];
        
        
        self.picsView = [[UIView alloc] initWithFrame:CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320, 70)];
        _picsView.backgroundColor = [UIColor clearColor];
        _picsView.userInteractionEnabled = YES;
        [self.contentView addSubview:_picsView];
        
        for (int i = 0; i<4; i++) {
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(8*(i+1)+70*i, 0, 70, 70)];
            imageV.tag = 1000+i;
            imageV.userInteractionEnabled = YES;
            imageV.backgroundColor = UIColorFromRGB(0xeaeaea);
            
            [_picsView addSubview:imageV];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(breviaryImageClick:)];
            [imageV addGestureRecognizer:tap];
        }
        
    }
    return self;
}

-(void)setDataDict:(NSDictionary *)dataDict
{
    if (_dataDict){
        _dataDict =nil;
    }
    _dataDict = [[NSMutableDictionary alloc] initWithDictionary:dataDict];
    [_headView sd_setImageWithURL:[NSURL URLWithString:_dataDict[@"Photo"]] placeholderImage:LOADIMAGE(@"placeholderHeader@2x")];
    _nameLabel.text = _dataDict[@"NickName"];
    _timeLabel.text = _dataDict[@"ReviewTime"];
    _starView.rating =[_dataDict[@"ReviewAvg"] floatValue];
    _countLabel.text = _dataDict[@"ReviewAvg"];
    _contentLabel.frame = CGRectMake(_headView.frame.origin.x,10+_headView.frame.size.height+_headView.frame.origin.y, 300, 23);
    _contentLabel.text = _dataDict[@"ReviewContent"];
    [_contentLabel sizeToFit];
    
    
    _picsView.frame =CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320, 70);
    CGFloat mheight = 20;
    switch ([_dataDict[@"ReviewPhoto"] count]) {
        case 0:
        {
            _picsView.hidden = YES;
            _picsView.frame =CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320,0);
            mheight = 20;
        }
            break;
        case 1:
        {
            _picsView.hidden = NO;
            _picsView.frame =CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320,70);
            mheight = 110;
            for (UIImageView * view in _picsView.subviews){
                if ([view isKindOfClass:[UIImageView class]]){
                    if (view.tag == 1000){
                        [view sd_setImageWithURL:[NSURL URLWithString:[_dataDict[@"ReviewPhoto"] firstObject]] placeholderImage:nil];
                        view.hidden = NO;
                    }else{
                        view.hidden = YES;
                    }
                }
            }
        }
            break;
        case 2:
        {
            _picsView.hidden = NO;
            _picsView.frame =CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320,70);
            mheight = 110;
            for (UIImageView * view in _picsView.subviews){
                if ([view isKindOfClass:[UIImageView class]]){
                    if (view.tag == 1000||view.tag ==1001){
                        view.hidden = NO;
                        [view sd_setImageWithURL:[NSURL URLWithString:[_dataDict[@"ReviewPhoto"] objectAtIndex:view.tag-1000]] placeholderImage:nil];
                    }else{
                        view.hidden = YES;
                    }
                }
            }
        }
            break;
        case 3:
        {
            _picsView.hidden = NO;
            mheight = 110;
            _picsView.frame =CGRectMake(0, _contentLabel.frame.origin.y+_contentLabel.frame.size.height+20,320,70);
            for (UIImageView * view in _picsView.subviews){
                if ([view isKindOfClass:[UIImageView class]]){
                    if (view.tag == 1003){
                        view.hidden = YES;
                    }else{
                        view.hidden = NO;
                        [view sd_setImageWithURL:[NSURL URLWithString:[_dataDict[@"ReviewPhoto"] objectAtIndex:view.tag-1000]] placeholderImage:nil];
                    }
                }
            }
        }
            break;
        case 4:
        {
            _picsView.hidden = NO;
            mheight = 110;
            for (UIImageView * view in _picsView.subviews){
                if ([view isKindOfClass:[UIImageView class]]){
                    view.hidden = NO;
                    [view sd_setImageWithURL:[NSURL URLWithString:[_dataDict[@"ReviewPhoto"] objectAtIndex:view.tag-1000]] placeholderImage:nil];
                }
            }
        }
            break;
        default:
            break;
    }
    
    _downLineView.frame = CGRectMake(0,height_y(_contentLabel)+mheight-.5, UI_SCREEN_WIDTH, .5);

}

-(void)breviaryImageClick:(UITapGestureRecognizer *)sender
{
    DDLOG_CURRENT_METHOD;
    UIImageView * iv =(UIImageView *) sender.view;
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    for (UIImageView * view in _picsView.subviews){
        if ([view isKindOfClass:[UIImageView class]]){
            if (!view.isHidden){
                [arr addObject:view];
            }
        }
    }
    [_delegate chooseImageView:iv andIvArray:arr andImageUrlArray:self.dataDict[@"ReviewPhoto"]];

    
    
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
